package com.headFirstModel.observerPattern.jdk;

import com.headFirstModel.observerPattern.custom.DisplayElement;

import java.util.Observable;
import java.util.Observer;

public class CurrentConditionsDisplay implements Observer,DisplayElement{

    private float temperature; //温度
    private float humidity;  //气压
    private Observable weatherData; //注册到的观察者

    public  CurrentConditionsDisplay(Observable weatherData){
        this.weatherData=weatherData;
        this.weatherData.addObserver(this);
    }

    @Override
    public void update(Observable o, Object arg) {
        if (o instanceof WeatherData) {
            WeatherData weatherData = (WeatherData) o;
            this.temperature = weatherData.getTemperature();
            this.humidity = weatherData.getHumidity();
            display();
        }
    }

    @Override
    public void display() {
        System.out.println("温度: " + temperature
                + "F, 湿度： " + humidity + "%");
    }
}
